<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 接收domain1的数据
      window.addEventListener(
        "message",
        function (e) {
          alert("data from domain1 ---> " + e.data);

          var data = JSON.parse(e.data);
          if (data) {
            data.number = 16;

            // 处理后再发回domain1
            window.parent.postMessage(
              JSON.stringify(data),
              "http://www.domain1.com"
            );
          }
        },
        false
      );
    </script>
  </body>
</html>
